package com.pingan.haofang.searchcloud.index.dao;

import com.pingan.haofang.searchcloud.index.domain.Index;
import com.pingan.haofang.searchcloud.quartz.dto.TaskQueryDto;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ex-sunbixiao001
 * @date 2018/6/21
 * @since 1.0.0
 */
public class IndexSpec {

    /**
     * 列信息
     */
    public static final String ID = "id";

    public static final String STATUS = "status";


    public static Specification<Index> listQuerySpec(TaskQueryDto dto) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (dto.getIndexStatus() != null) {
                predicates.add(builder.equal(root.get(STATUS), dto.getIndexStatus()));
            }
            if (dto.getIndexId() != null) {
                predicates.add(builder.equal(root.get(ID), dto.getIndexId()));
            }

            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
    }
}
